package com.example.musicwebcon.service;

import com.example.musicwebcon.model.favorSong;
import com.example.musicwebcon.repository.favorRepository;
import com.example.musicwebcon.model.dto.favorDTO;
import com.example.musicwebcon.service.serviceInterface.IfavorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class favorService implements IfavorService {
    @Autowired
    favorRepository favorRepository;

    @Override
    public Iterable<favorSong> getALl() {
        return favorRepository.findAll();
    }

    @Override
    public favorSong add(favorDTO favorSong) {
        favorSong favorsong = new favorSong();
        BeanUtils.copyProperties(favorSong,favorsong);
        return favorRepository.save(favorsong);
    }

    @Override
    public favorSong getMusicId(String musicId) {
        return favorRepository.findById(musicId).orElseThrow(() -> {
            throw new IllegalArgumentException("歌曲不存在，参数异常！");
        });
    }

    @Override
    public favorSong edit(favorDTO favorSong) {
        favorSong favorsong = new favorSong();
        BeanUtils.copyProperties(favorSong,favorsong);
        return favorRepository.save(favorsong);
    }

    @Override
    public void deleteMusicId(String musicId) {
        favorRepository.deleteById(musicId);
    }
}
